App({

// 获取用户id
  
  /**
   * 当小程序初始化完成时，会触发 onLaunch（全局只触发一次）
   */
  onLaunch: function () {
    
  },

  getshoppingList: function () {
    let that = this;
    wx.request({
      url: 'https://www.xiaozheng8.com/shop/cart/findByUserId',
      method: 'post',
      data: {
        userId: wx.getStorageSync("userId"),
      },
      success: function (res) {
        console.log(res)
        let arr = res.data.data.pic;
        that.globalData.num=res.data.data.length;
        console.log(that.globalData.num)
        if (that.globalData.num == 0) {
          wx.removeTabBarBadge({
            index: 1,
          });
        } else {
          console.log(that.globalData.num);
          wx.setTabBarBadge({
            index: 1,
            text: ""+that.globalData.num+""
          })
        }
      }
    })
  },
  globalData:{
    openId:"",
    num:""
  },
  /**
   * 当小程序启动，或从后台进入前台显示，会触发 onShow
   */
  onShow: function (options) {
    wx.login({
      success: function(res) {
        console.log(res.code)
      wx.request({
        url: 'https://www.xiaozheng8.com/shop/login',
        data: {
          code:res.code
        },
        method: 'post',
        success: function(res) {
            console.log("=====")
          console.log(res);
          wx.setStorageSync("userId", res.data.data.userId);
        },
        fail: function(res) {
          console.log(res);
        },
        complete: function(res) {},
      })
      },
      fail: function(res) {},
      complete: function(res) {},
    });
    this.getshoppingList();
  },

  /**
   * 当小程序从前台进入后台，会触发 onHide
   */
  onHide: function () {
    
  },

  /**
   * 当小程序发生脚本错误，或者 api 调用失败时，会触发 onError 并带上错误信息
   */
  onError: function (msg) {
    
  }
})
